package com.singzi.leetcode.N100;

import org.junit.Test;

public class T0024 {
    public static void main(String[] args) {
        ListNode l5 = generateList();


        Solution0024 solution0024 = new Solution0024();
        printList(solution0024.swapFirstTwo(l5));
        System.out.println("---------------------");
////        ListNode listNode1 = solution0024.swapPairs(l2);
//        Solution00243 solution00243 = new Solution00243();
//        printList(l5);
//        printList(solution00243.swapThreePairs(l5));
//        printList(solution00243.swapFirstThree(l5));

    }
    @Test
    public void printThreeList(){

        ListNode l5 = generateList();
        Solution00243 solution00243 = new Solution00243();
        printList(solution00243.swapFirstThree(l5));

    }
    @Test
    public void printSwapThreeList(){

        ListNode list = generateList();
        printList(list);
        System.out.println("___________________________");
        Solution00243 solution00243 = new Solution00243();
        printList(solution00243.swapThreePairs(list));

    }
    public static void printList(ListNode list){
        while (list!=null){
            System.out.println(list.val);
            list = list.next;
        }
    }
    public static ListNode generateList(){
        ListNode listNode = new ListNode(4);
        ListNode l1 = new ListNode(5, listNode);
        ListNode l2= new ListNode(6, l1);
        ListNode l3= new ListNode(7, l2);
        ListNode l4= new ListNode(8, l3);
        ListNode l5= new ListNode(9, l4);
        ListNode l6= new ListNode(10, l5);
        ListNode l7= new ListNode(11, l6);
        ListNode l8= new ListNode(12, l7);
        ListNode l9= new ListNode(13, l8);
        ListNode l10= new ListNode(14, l9);
        ListNode l11= new ListNode(15, l10);
        ListNode l12= new ListNode(16, l11);
        return l12;

    }
}
//交换两个节点
class Solution0024 {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode list = head.next;
        head.next = swapPairs(list.next);
        list.next = head;
        return list;
    }
    public ListNode swapFirstTwo(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode list = head.next;
        head.next = list.next;
        list.next = head;

        return list;

    }
}
//交换三个节点
class Solution00243 {
    public ListNode swapThreePairs(ListNode head) {
        if (head == null || head.next == null||head.next.next == null) {
            return head;
        }
        ListNode list = head.next.next;
        ListNode temp = head.next;
        head.next = swapThreePairs(list.next);
        list.next = temp;
        temp.next = head;



        return list;
    }
    public ListNode swapFirstThree(ListNode head){
        if (head == null || head.next == null||head.next.next == null) {
            return head;
        }

//        ListNode temp = head.next;
//        ListNode list = head.next.next;
//        head.next = list.next;
//        list.next = temp;
//
//        temp.next = head;


        ListNode list = head.next.next;
        list.next = head.next;
        list.next.next = head;
        head.next = list.next;


        return list;
    }
}
